/****************************************************************************
* 			Title: STYL Construction Files
*			Output: 
*			Date:   
*****************************************************************************/
 
////1. DIRECTORY INFORMATION ////

	//a. Path to change		
	
		gl UY "..."
		
	//b. Set directory
	
		cd "$UY"
		
	//c. Open dataset
		clear all
		set maxvar 27000
		use "$UY/STYL_Final_old", clear

//// 3. SURVEYSET, PROGRAMS AND GLOBALS ////
	
	//a. SET SURVEY

	svyset, strata(strata) psu(partid)
	
	//b. SET GLOBALS
 
		* Controls
			gl cohorts "redlight_p1 redlight_p2 centralmonrovia_p2 redlight_p3 centralmonrovia_p3 claratown_p3 logantown_p3 newkrutown_p3 "
			gl strata "I.tp_strata_alt I.cg_strata"
			
			# delimit ;
			gl base "age_b livepartner_b mpartners_b hhunder15_b 
				famseeoften_b  muslim_b school_b schoolbasin_b 
				literacy_b mathscore_b health_resc_b disabled_b depression_b distress_b
				rel_commanders_b  faction_b warexper_b
				
				profitsump99avg7d_b wealth_indexstd_b homeless_b slphungry7dx_b   
				savstockp99_b loan50_b loan300_b
				
				illicit7da_zero_b agricul7da_zero_b nonagwage7da_zero_b allbiz7da_zero_b 
				nonaghigh7da_zero_b agriculeveramt_b nonagbizeveramt_b nonaghigheveramt_b 
				
				drugssellever_b drinkboozeself_b druggrassself_b grassdailyuser_b 
				harddrugsever_b harddrugsdailyuser_b 
				
				steals_b stealnb_nonviol_b stealnb_felony_b  
				disputes_all_b
				asbhostil_b 
				
				conscientious_b neurotic_b grit_b rewardresp_b locuscontr_b 
				impulsive_b selfesteem_b
				
				patient_game_real_b inconsistent_game_resc_b risk_game_resc_b
				timedecl_b riskdecl_b
				
				cognitive_score_b ef_score_b";
			
			gl dvs_t1 "fam_econ fam_asb mech_allexceptef timepref selfcontrolnolo fam_identity fam_mental fam_substance fam_network";
			gl dvs_t2 "fam_asb drugssellever stealnb disputes_all_z carryweapon arrested asbhostilstd domabuse_z";
			gl dvs_t3 "fam_econ cstot2wusd profitsump99avg7d wealth_indexstd savstockp99 bizexptotp99 bizstocktotp99 houravg7d homeless  victimany";
			gl dvs_t4 "timepref timepref_p timepref_t";
			gl dvs_t5 "selfcontrolnolo impulsivestd  conscientiousstd gritstd rewardrespstd";
			gl dvs_t6 "fam_identity violatt appearanceindex prosocialindex";
			gl dvs_t7 "fam_mental fam_mental_pos fam_mental_dep";
			gl dvs_t8 "fam_substance drinkboozeever grassever harddrugsever";
			gl dvs_t9 "fam_network peerqualitystd famsupportindex rel_commanders patronlvl";
			gl dvs_t13 "fam_mental fam_mental_pos selfesteemstd wellbeing locuscontrstd neuroticstd  fam_mental_dep distress depression";
			gl dvs_t10 "personalitystdg1 personalitystdle1 violatt_g1 violatt_le1";
			gl dvs_d7 "personalitystd timepref ef_score	sctp scef sctpef";
			
						
			gl dvs_d1 " fam_asb
					drugssellever 
					stealnb 
						stealtakethingsx stealmistakecorrx stealscrapex stealpickpocketx 
						stealscamx stealblackdeedx stealmuggerx stealarmedrobx 
					disputes_all_z 
						fightsmlneigh fightbigneigh fightphys fightweapon fightsmlleader 
						fightbigleader fightsmlpolice fightbigpolice fine			
					carryweapon 
					arrested 
					asbhostilstd
					badreact_resc badadvice_resc hardjokes_resc baddestroy_resc cheat_resc 		
								troublesome_resc frustrated_resc threaten_resc takethings_resc 												
								selfdiscipline_resc asb_reactlose_resc asb_reacthit_resc 					
								asb_reactdefend_resc asb_reacttease_resc asb_proontop_resc 						
								asb_provandalize_resc asb_procompetitor_resc asb_prophysforce_resc 				
								asb_procuss_resc
					domabuse_z
						maccuse_resc mthreat_resc minsult_resc mhit_resc
					";
											
			gl dvs_d2 "selfcontrolnolo
						impulsivestd 
							bimpls_buy_resc bimpls_act_resc bimpls_talk_resc bimpls_squirm_resc bimpls_concentrate_resc 
							bimpls_present_resc bimpls_control_resc time_decl_regretspend_resc
						conscientiousstd
							cnsc_prepare_resc cnsc_details_resc cnsc_doitright_resc cnsc_plans_resc 
							cnsc_getby_resc cnsc_work_resc cnsc_shirk_resc cnsc_complete_resc
						gritstd 
							prsv_overcome_resc prsv_ambitious_resc prsv_discourage_resc prsv_achieveimp_resc 
							prsv_driven_resc prsv_changeplan_resc prsv_achievenoimp_resc
						rewardrespstd 
							bas_goallout_resc bas_noholds_resc bas_doforfun_resc bas_keepatit_resc bas_feelexcite_resc 
							bas_seeopp_resc bas_feelstrongly_resc bas_wincontest_resc"; 
			
			gl dvs_d3 "timepref
						 timepref_p
							patient_game_real
							patient_game_decl
							d_rate
							timedecl_p
								time_decl_patienceladder time_decl_patient_resc time_decl_saveproblems_resc
						  timepref_t
							inconsistent_game_real 
							inconsistent_game_decl
							inconsistent_rate_dsct
							timedecl_t
								time_decl_spendquick time_decl_celebrate th_avdmnywstingfrnds_resc 
					  timepref_real";
					  
			gl dvs_d4 "fam_identity
						violatt
							attviolencestd 
								nvstrangerrob nvowetakesthg nvknowncriminal nvstorekeeperpunish_resc nvkillstore_resc 
								nvbeatwoman nvdestroyproperty nvstealfriendsgirl nvcorruptleaderbeaten nvbeatwife nvgirlrape
							attcriminalitystd 
								attstchinamanbeat attstblackmoney attstnightops_resc attsttaxiwallet 
								attstmoneydroptake attstfamilylec attstmoneybagtake attstmoneyhangtake 
								attstroomvaluablestake attselfdrugsell attstknownindiv attstifreturn
							politicalviolstd 
								pvhypdemstpaid  pvhyppalpaid pvhyppalpaid200ld 
								pvhypdemstpaidwcheat pvhyppalpaidwcheat
								pvhyppalpaid200ldwcheat
						appearanceindex
							clothescond_resc shoecond_resc breath_resc cleanface_resc 
							hair_resc fingernails_resc
						prosocialindex
							groups 
							groupleadindex
								leader bigman groupcommleader startgroup_resc
							publicgoodcontrnumb 
							trustindex
								trustrelatives_resc trusttleader_resc trustngos_resc trustipal_resc";

							
			gl dvs_d5 " fam_mental
							fam_mental_pos
								selfesteemstd
									se_satisfied_resc  se_useless_resc se_fail_resc se_respect_resc 
									se_goodperson_resc se_good4nothing_resc se_bizwell_resc se_notproud_resc
								wellbeing
									laddernow
									subwellbeing
										happiness satisfaction health
									relwellbeing
										ladderrich ladderresp ladderpower ladderaccess
								locuscontrstd
									lc_choicesfuture_resc lc_nocontrol_resc lc_bizluck_resc lc_tryhard_resc 
									lc_planswork_resc lc_badluck_resc lc_peoplestreets_resc lc_success_resc
								neuroticstd
									nrtc_sad_resc nrtc_worry_resc nrtc_threaten_resc nrtc_offend_resc 
									nrtc_stress_resc nrtc_disturb_resc nrtc_relax_resc nrtc_bother_resc
							fam_mental_dep
								depression
									baddo_resc badhear_resc badsad_resc badtired_resc badeat_resc badimportant_resc
								distress
									thinkbad_resc baddream_resc badagain_resc badbody_resc  badaway_resc badtalk_resc 
									badspoiled_resc badfrust_resc badspirit_resc baddry_resc badsat_resc badburn_resc	
					subabuse
							drinkboozeever grassever harddrugsever
					socialnetwork
						peerqualitystd 
							peerschool peercommmtg peerchurch peerwork peerbusiness peersave 
							peertrust peershare peercheer peerguard
							peerbeg_resc peerdrunk_resc peerdrugs_resc peerpickpocket_resc 
							peerburglary_resc  peerrobbers_resc peergamble_resc peerexcom_resc 
							peercomdr_resc peersmallpal_resc peerbigpal_resc
						famsupportindex
							famseeoften_resc famconcern_resc famadvise_resc famhelp_resc
						rel_commanders
							peercomdr relcomm jobcomm repcomm
						patronlvl
							ptrnjob ptrnbiz ptrnfood ptrnschl  ptrnhsing
					ef_score
							ef_arrowtime A_2timesec_resid A_3timesec_resid
							ef_arrowerror  A_2error_resid A_3error_resid
							ef_maze maze3firsttouch_std maze3backtrack_std_resc
							backdigscore_resid";				
					
			gl dvs_e7 "houravg7d nonaghighhouravg7d nonagbizhouravg7d nonagwagehouravg7d
							agriculhouravg7d illicithouravg7d";			
		
		
		# delimit cr;

			qui la var tpassonly "TP Only"
			qui la var cashassonly "Cash Only"
			qui la var tpcashass "TP and Cash"
			
		/* Generate fixed effects dummies */
			qui tab tp_strata_alt, g(tp_dummy)
			qui tab cg_strata, g(cg_dummy)
			qui tab neighborhoodphase, g(nbhd_dummy)
 
quietly reg fam_asb_lt tpassonly cashassonly tpcashass ${base} ${strata} if (round == 5) & unfound_wave == 0, r	

mat betas = e(b)
gen TO = betas[1,1]
gen CO = betas[1,2]
gen B = betas[1,3]
mat Sigma = e(V)
gen Var_TO = Sigma[1,1]
gen Var_CO = Sigma[2,2]
gen Var_B = Sigma[3,3]
gen Cov_TO_CO = Sigma[1,2]
gen Cov_TO_B = Sigma[1,3]
gen Cov_CO_B = Sigma[2,3]

outsheet TO CO B Var_TO Var_CO Var_B Cov_TO_CO Cov_TO_B Cov_CO_B using RegressionOutput.csv if surveyid == 200011, replace

drop TO CO B Var_TO Var_CO Var_B Cov_TO_CO Cov_TO_B Cov_CO_B

keep if e(sample) == 1

capture program drop myboot
program define myboot, rclass
 preserve 
  bsample
	quietly reg fam_asb_lt tpassonly cashassonly tpcashass ${base} ${strata}, r	
	mat betas = e(b)
	return scalar TO = betas[1,1]
	return scalar CO = betas[1,2]
	return scalar B = betas[1,3]
	mat Sigma = e(V)
	return scalar Var_TO = Sigma[1,1]
	return scalar Var_CO = Sigma[2,2]
	return scalar Var_B = Sigma[3,3]
	return scalar Cov_TO_CO = Sigma[1,2]
	return scalar Cov_TO_B = Sigma[1,3]
	return scalar Cov_CO_B = Sigma[2,3]
 restore
end

simulate TO=betas[1,1] CO=betas[1,2] B=betas[1,3] Var_TO=Sigma[1,1] ///
	Var_CO=Sigma[2,2] Var_B=Sigma[3,3] Cov_TO_CO=Sigma[1,2] ///
	Cov_TO_B=Sigma[1,3] Cov_CO_B=Sigma[2,3], reps(10000) seed(1): myboot
	
outsheet TO CO B Var_TO Var_CO Var_B Cov_TO_CO Cov_TO_B Cov_CO_B using SimulationOutput.csv, replace


	
